package com.xiaoq.flowrate.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaoq.flowrate.entity.ExtIfEntity;
import com.xiaoq.flowrate.repository.IExtIfRepository;
import com.xiaoq.flowrate.service.IFlowrateService;
import com.xiaoq.flowrate.sitesInterface.SitesFlowrate;
import com.xiaoq.sites.vo.TouristStaticsVo;

@Component
public class FlowrateServiceImpl implements IFlowrateService{
	
	private static final Logger LOG = LoggerFactory.getLogger(FlowrateServiceImpl.class);
	
	@Autowired
	private IExtIfRepository iextIfRep;

	@Override
	public TouristStaticsVo findTouristStatics(String sitesId,Long capacity) {
		TouristStaticsVo tourst = new TouristStaticsVo();
		if(StringUtils.isNotEmpty(sitesId) && capacity!=null && capacity!=0){
			ExtIfEntity extIf = iextIfRep.findExtIf(sitesId);
			if (extIf != null) {
				String ifBean = extIf.getIfBean();
				String ifUrl = extIf.getIfUrl();
				String ifConfig = extIf.getIfConfig();

				try {
					Object obj = Class.forName(ifBean).newInstance();
					SitesFlowrate sitesf = (SitesFlowrate) obj;
					// 即使不是REST格式的是webservice格式的那么配置信息也是xml格式字符串
					// 景区对应的地方进行换成xml解析就行了
					tourst = sitesf.getSitesFlowrate(ifUrl, ifConfig,capacity);
				} catch (InstantiationException e) {
					LOG.error("类的实例化异常", e);
				} catch (IllegalAccessException e) {
					LOG.error("非法运行异常", e);
				} catch (ClassNotFoundException e) {
					LOG.error("类的强制转化出错", e);
				}
			}else{
				tourst.setCount(-1L);
				tourst.setCongestionIdx(-1);
			}
		}else{
			tourst.setCount(-1L);
			tourst.setCongestionIdx(-1);
		}
		

		return tourst;
	}

}
